package com.emapp.lccode.activity

import android.app.DatePickerDialog.OnDateSetListener
import android.os.Bundle
import android.widget.DatePicker
import com.emapp.lccode.R
import com.emapp.lccode.adapter.AdminCityAdapter
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.base.BaseConfig
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.bean.Project
import com.emapp.lccode.databinding.ActivityAdminCityListBinding
import com.emapp.lccode.mode.ProjectViewModel
import com.emapp.lccode.utils.MmkvUtil
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.emapp.lccode.utils.vertical
import com.gyf.immersionbar.ktx.immersionBar
import com.hjq.toast.Toaster
import com.mylhyl.circledialog.CircleDialog
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlin.system.exitProcess

/**
 * 管理员切换地区（内部专用）
 */
class AdminListActivity : BaseActivity1<ProjectViewModel, ActivityAdminCityListBinding>(),
    OnDateSetListener {
    var datas: ArrayList<Project> = arrayListOf()
    private val adminCityAdapter: AdminCityAdapter by lazy { AdminCityAdapter() }
    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            titleBar(mViewBind.lvTitle.ivBar)
            statusBarDarkFont(true)
        }
        mViewBind.lvTitle.tvTitle.text = "切换地区"
        mViewBind.lvTitle.tvRight.text = ""
        EventBus.getDefault().register(this)
        mViewBind.rvList.run {
            vertical()
            adapter = adminCityAdapter
        }
        adminCityAdapter.run {
            setOnItemClickListener { adapter, _, position ->
                val item = adapter.items[position]
                if (item.name == "济南市") {
                    Toaster.show("未开放")
                    return@setOnItemClickListener

                }
                CircleDialog.Builder().setTitle("切换app地区:" + item.name).setSubTitle("重启app生效").setPositive("确定") {
                    MmkvUtil.putInt("city_version", item.id.toInt())
                    MmkvUtil.putBoolean("isAutoLogin", false)
                    EventBus.getDefault().post(EventBusModel(EventBusConfig.FINISH_APP))
                    exitProcess(0)
                    true
                }.show(supportFragmentManager)
            }
            isEmptyViewEnable = true
            emptyView = layoutInflater.inflate(R.layout.listitem_empty, mViewBind.rvList, false)
        }
        mViewBind.refreshLayout.run {
            setRefreshHeader(ClassicsHeader(this@AdminListActivity))
            setRefreshFooter(ClassicsFooter(this@AdminListActivity))
            setOnRefreshListener {
                finishRefreshWithNoMoreData()
            }
            setOnLoadMoreListener {
                finishLoadMoreWithNoMoreData()
            }
        }
        adminCityAdapter.submitList(BaseConfig.versions)
    }


    override fun createObserver() {

    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
            mViewBind.lvTitle.ivLeft,
            mViewBind.lvTitle.tvRight
        ) {
            when (it.id) {
                R.id.iv_left -> {
                    finish()
                }

                R.id.tv_right -> {
                }

            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            EventBusConfig.FINISH -> {
            }

            else -> {}
        }
    }


    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
    }

}
